package com.hospital.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hospital.mapper.GhxxbMapper;
import com.hospital.mapper.KsbMapper;
import com.hospital.mapper.YsbMapper;
import com.hospital.pojo.*;
import com.hospital.vo.BaseQueryVo;
import com.hospital.vo.GuahaoResultVo;
import com.hospital.vo.ResultVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class HospInitQueryService {
    //挂号信息表mapper
    @Resource
    private GhxxbMapper ghxxbMapper;

    //科室表mapper
    @Resource
    private KsbMapper ksbMapper;

    //医生表mapper
    @Resource
    private YsbMapper ysbMapper;

    /**
     * 按照查询条件模糊查询挂号信息列表
     * @param pageNum
     * @param pageSize
     * @param vo
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public PageInfo<GuahaoResultVo> getGuahaoInfo(Integer pageNum, Integer pageSize, BaseQueryVo vo){
        PageHelper.startPage(pageNum, pageSize);
        //指定排序字段和升序还是降序
        PageHelper.orderBy("ghsj desc");
        List<GuahaoResultVo> guahaoResultVos = ghxxbMapper.multiQuery(vo);
        return new PageInfo<>(guahaoResultVos);
    }

    /**
     * 查看某一个病历号的详情
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public Ghxxb getGhxxbByBlh(Integer blh){
        GhxxbExample ghxxbExample = new GhxxbExample();
        GhxxbExample.Criteria criteria = ghxxbExample.createCriteria();
        criteria.andBlhEqualTo(blh);
        List<Ghxxb> ghxxbs = ghxxbMapper.selectByExample(ghxxbExample);
        if (ghxxbs.size() < 1){
            System.out.println("query by blh returns 0");
            return null;
        }else if (ghxxbs.size() > 1){
            System.out.println("query by blh returns more than one object");
            return null;
        }
        return ghxxbs.get(0);
    }

    /**
     * 获取科室信息
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Ksb> getKeshiInfo(){
        KsbExample ksbExample = new KsbExample();
        List<Ksb> ksbs = ksbMapper.selectByExample(ksbExample);
        return ksbs;
    }

    /**
     * 根据医生ID获取其对应的科室
     * @param docId
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Ksb> getKeshiInfo(Integer docId){
        Ksb ksByDoc = ghxxbMapper.getKsByDoc(docId);
        ArrayList<Ksb> ksbs = new ArrayList<>();
        ksbs.add(ksByDoc);
        return ksbs;
    }

    /**
     * 获取医生信息
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Ysb> getDocInfo(){
        YsbExample ysbExample = new YsbExample();
        List<Ysb> ysbs = ysbMapper.selectByExample(ysbExample);
        return ysbs;
    }
    /**
     * 获取医生信息
     */
    @Transactional(propagation = Propagation.REQUIRED, readOnly = true)
    public List<Ysb> getDocInfo(Integer ksId){
        YsbExample ysbExample = new YsbExample();
        YsbExample.Criteria criteria = ysbExample.createCriteria();
        criteria.andKsidEqualTo(ksId);
        List<Ysb> ysbs = ysbMapper.selectByExample(ysbExample);
        return ysbs;
    }

    /**
     * 退号
     * @param blh
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int tuiHao(Integer blh) {
        Ghxxb ghxxb = new Ghxxb();
        ghxxb.setBlh(blh);
        ghxxb.setZt(0);
        int update = ghxxbMapper.updateByPrimaryKeySelective(ghxxb);
        return update;
    }

    /**
     * 更新挂号信息
     * @param ghxxb
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int updateGhxxb(Ghxxb ghxxb){
        int update = ghxxbMapper.updateByPrimaryKeySelective(ghxxb);
        return update;
    }

    /**
     * 批量退号
     * @param blhs
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int multiTuihao(int[] blhs){
        int i = ghxxbMapper.multiTuihao(blhs);
        return i;
    }

    /**
     * 批量退院
     * @param blhs
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int multiTuiyuan(List blhs){
        int i = ghxxbMapper.multiTuiyuan(blhs);
        return i;
    }

    /**
     * 新增挂号信息
     * @param ghxxb
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public int addGhxx(Ghxxb ghxxb) {
        int insert = ghxxbMapper.insert(ghxxb);
        return insert;
    }

    public int multiChuyuan(List blhs) {
        int i = ghxxbMapper.multiChuyuan(blhs);
        return i;
    }
}
